#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
#   GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
#   Author :  Shawn.Tan
#   Date : 2025.10.28
#
#   History : Initial Version 2025.10.28
#
#
from base.SortableObject import SortableObject


#
# Provide a Macro to wrap a non-hashable object (dictionary for example)
# so it can be hashable and comparable (eq, lt, and gt) with other object
#
class Macro(SortableObject):
    def __init__(self, objName, objVal):
        super().__init__()
        self.objName = objName
        self.objVal = objVal
        self.sortableName = self.serialize()

    def obj(self):
        return self.objName

    def value(self):
        return self.objVal

    def serialize(self):
        tmp_str = str(self.objName)
        if isinstance(self.objVal, dict):
            for k, v in sorted(self.objVal.items()):
                if isinstance(k, str):
                    tmp_str += k
                elif isinstance(k, int):
                    tmp_str += str(k)
                else:
                    raise TypeError
                if isinstance(v, str):
                    tmp_str += v
                elif isinstance(v, int):
                    tmp_str += str(v)
        elif isinstance(self.objVal, str):
            tmp_str += self.objVal
        else:
            raise TypeError
        return tmp_str
